package com.ruoyi.system.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

@Service
public class GatherDataUtil {
    @Value("${gather.host}")
    private String host;

    private String START_GATHER_DATA_EVENT = "start_gather_data_event/";

    private String START_GATHER_DATA_TOPIC = "start_gather_data_topic/";



    private String requestNlpc(String url, LinkedMultiValueMap paras){
        url = host + url;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();

        factory.setConnectTimeout(30 * 1000);
        factory.setReadTimeout(30 * 1000);

        RestTemplate restTemplate = new RestTemplate(factory);
        HttpEntity requestBody = new HttpEntity(paras, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestBody, String.class);
        String result = responseEntity.getBody();

        return result;
    }


    public String startGatherData(Long eventId) {
        LinkedMultiValueMap paras = new LinkedMultiValueMap();
        paras.add("eventId", eventId);
        String result = requestNlpc(START_GATHER_DATA_EVENT, paras);
        return result;
    }

    public String startGatherDataByTopic(Long topicId) {
        LinkedMultiValueMap paras = new LinkedMultiValueMap();
        paras.add("topicId", topicId);
        String result = requestNlpc(START_GATHER_DATA_TOPIC, paras);
        return result;
    }
}
